package com.ziyia.notification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private NotificationManager manager;
    private Notification tz1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 通知
        // 获取通知管理器
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "nsyw", "测试通知", NotificationManager.IMPORTANCE_HIGH
            );
            manager.createNotificationChannel(channel);
        }

        Intent intent = new Intent(this, NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        tz1 = new NotificationCompat.Builder(this, "nsyw")
                // 设置通知标题
                .setContentTitle("我是标题")
                // 设置通知内容
                .setContentText("年少有为")
                // 添加小图标
                .setSmallIcon(R.drawable.ic_baseline_close_24)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.image))
                // 设置小图标的颜色
                .setColor(Color.parseColor("#FF0000"))
                // 设置点击通知后的意图
                .setContentIntent(pendingIntent)
                // 设置点击通知后清除通知
                .setAutoCancel(true)
//                 .setWhen(10000)
                .build();

    }



    public void setTz(View view) {
        manager.notify(1, tz1);
    }

    public void OuttZ(View view) {
        manager.cancel(1);
    }
}